package controller

import (
	"base/app/models"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"strings"
)

type chartCtl struct {
	db *gorm.DB
}

var Chart = chartCtl{}

type chartOneReq struct {
	StartDate string `json:"startDate"`
	EndDate   string `json:"endDate"`
}
type DayData struct {
	DateOnly      string  `json:"date_only"`
	TotalWeight   float64 `json:"total_weight"`
	TotalDisposal float64 `json:"total_disposal"`
}

// 按天查询、获取每天的总打捞量、处置量
func (ctl *chartCtl) GetDayData(c *gin.Context) {
	ctl.db = models.Db
	var req chartOneReq
	_ = c.ShouldBind(&req)
	//查询指定日期范围weight和disposal
	var dayData []DayData
	ctl.db.Table("record").
		Select("DATE(date) as date_only, SUM(weight) as total_weight, SUM(disposal) as total_disposal").
		Where("DATE(date) >= ? AND DATE(date) <= ? AND state =?", req.StartDate, req.EndDate, "完成").
		Group("date_only").
		Scan(&dayData)

	//dayData中DateOnly格式化为yyyy-mm-dd
	for i := 0; i < len(dayData); i++ {
		dayData[i].DateOnly = dayData[i].DateOnly[0:10]
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "获取成功",
		"data": dayData,
	})

}

//func (ctl *chartCtl) GetDayData(c *gin.Context) {
//	ctl.db = models.Db
//	var req chartOneReq
//	_ = c.ShouldBind(&req)
//
//	req.StartDate = strings.ReplaceAll(req.StartDate, "T", " ")
//	req.EndDate = strings.ReplaceAll(req.EndDate, "T", " ")
//	//	获取该段时间内，状态为完成的所有打捞量和处置量
//	var dayData []DayData
//	ctl.db.Table("record").
//		Select("SUM(weight) as total_weight, SUM(disposal) as total_disposal").
//		Where("DATE_FORMAT(date, '%Y-%m-%d %H') >= ? AND DATE_FORMAT(date, '%Y-%m-%d %H') <= ? AND state =?", req.StartDate, req.EndDate, "完成").
//		Scan(&dayData)
//
//	c.JSON(200, gin.H{
//		"code": 200,
//		"msg":  "获取成功",
//		"data": dayData,
//	})
//
//}

// 按照打捞海域查询、统计、导出某段时间打捞量(Weight)
type chartTwoReq struct {
	StartDate string `json:"startDate"`
	EndDate   string `json:"endDate"`
}
type AreaData struct {
	AreaName    string  `json:"area_name"`
	TotalWeight float64 `json:"total_weight"`
}

func (ctl *chartCtl) GetAreaData(c *gin.Context) {
	ctl.db = models.Db
	var req chartTwoReq
	_ = c.ShouldBind(&req)
	//查询指定日期范围weight和disposal
	var areaData []AreaData
	ctl.db.Table("record").
		Select("area_name, SUM(weight) as total_weight").
		Where("DATE(date) >= ? AND DATE(date) <= ? AND state =?", req.StartDate, req.EndDate, "完成").
		Group("salvageArea").
		Scan(&areaData)

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "获取成功",
		"data": areaData,
	})
}

type chartThreeReq struct {
	StartDate string `json:"startDate"`
	EndDate   string `json:"endDate"`
	ShipName  string `json:"ship_name"`
}

type ShipData struct {
	DateOnly    string  `json:"date_only"`
	ShipName    string  `json:"ship_name"`
	TotalWeight float64 `json:"total_weight"`
}

// 按驳船或者渔船号统计查询、统计、导出某段时间卸载量(Weight)
func (ctl *chartCtl) GetShipData(c *gin.Context) {
	ctl.db = models.Db
	var req chartThreeReq
	req.StartDate = strings.Split(req.StartDate, "T")[0]
	req.EndDate = strings.Split(req.EndDate, "T")[0]
	_ = c.ShouldBind(&req)
	//查询指定日期范围weight和disposal
	var shipData []ShipData
	//如果req.ShipName包含“渔”，当作渔船号查询(fisherShip),否则当作驳船号查询(bargeShip)
	if strings.Contains(req.ShipName, "渔") {
		ctl.db.Table("record").
			Select("DATE(date) as date_only, fisher_ship as ship_name, SUM(weight) as total_weight").
			Where("DATE(date) >= ? AND DATE(date) <= ? AND state =? AND fisher_ship = ?", req.StartDate, req.EndDate, "完成", req.ShipName).
			Group("date_only,ship_name").
			Scan(&shipData)
	} else {
		ctl.db.Table("record").
			Select("DATE(date) as date_only, barge_ship as ship_name, SUM(weight) as total_weight").
			Where("DATE(date) >= ? AND DATE(date) <= ? AND state =? AND barge_ship = ?", req.StartDate, req.EndDate, "完成", req.ShipName).
			Group("date_only,ship_name").
			Scan(&shipData)

	}

	for i := 0; i < len(shipData); i++ {
		shipData[i].DateOnly = shipData[i].DateOnly[0:10]
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "获取成功",
		"data": shipData,
	})
}

// 驳船按日统计卸载船次
type chartFourReq struct {
	StartDate string `json:"startDate"`
	EndDate   string `json:"endDate"`
	BargeShip string `json:"bargeShip"`
}

type BargeShipData struct {
	DateOnly   string `json:"date_only"`
	TotalCount int    `json:"total_count"`
}

func (ctl *chartCtl) GetBargeShipData(c *gin.Context) {
	ctl.db = models.Db
	var req chartFourReq
	_ = c.ShouldBind(&req)
	//查询指定日期范围weight和disposal
	var bargeShipData []BargeShipData
	ctl.db.Table("record").
		Select("DATE(date) as date_only, barge_ship as bargeShip, COUNT(*) as total_count").
		Where("DATE(date) >= ? AND DATE(date) <= ? AND state =? AND barge_ship = ?", req.StartDate, req.EndDate, "完成", req.BargeShip).
		Group("date_only,bargeShip").
		Scan(&bargeShipData)

	for i := 0; i < len(bargeShipData); i++ {
		bargeShipData[i].DateOnly = bargeShipData[i].DateOnly[0:10]
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "获取成功",
		"data": bargeShipData,
	})
}

// 按照打捞海域获取一段时间内的各个海域的总打捞量
func (ctl *chartCtl) GetAreaDataByDate(c *gin.Context) {
	ctl.db = models.Db
	var req chartOneReq
	_ = c.ShouldBind(&req)
	req.StartDate = strings.ReplaceAll(req.StartDate, "T", " ")
	req.EndDate = strings.ReplaceAll(req.EndDate, "T", " ")
	var areaData []AreaData
	ctl.db.Table("record").
		Select("salvage_area as area_name, SUM(weight) as total_weight").
		Where("DATE_FORMAT(date, '%Y-%m-%d %H') >= ? AND DATE_FORMAT(date, '%Y-%m-%d %H') <= ? AND state =?", req.StartDate, req.EndDate, "完成").
		Group("salvage_area").
		Scan(&areaData)

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "获取成功",
		"data": areaData,
	})
}

// 传入驳船号和日期，获取该驳船号在该日期的总打捞量和历史全部打捞量
type BargeShipDataQj struct {
	Date      string `json:"date"`
	BargeShip string `json:"bargeShip"`
}

func (ctl *chartCtl) GetBargeShipDataByDate(c *gin.Context) {
	ctl.db = models.Db
	var req BargeShipDataQj
	_ = c.ShouldBind(&req)
	//查询指定日期范围weight和disposal
	var bargeShipData []BargeShipData
	ctl.db.Table("record").
		Select("DATE(date) as date_only, barge_ship as bargeShip, SUM(weight) as total_weight").
		Where("DATE(date) = ? AND barge_ship = ?", req.Date, req.BargeShip).
		Group("date_only,bargeShip").
		Scan(&bargeShipData)

	//查询历史全部打捞量
	var totalWeight float64
	ctl.db.Table("record").
		Select("SUM(weight) as total_weight").
		Where("barge_ship = ?", req.BargeShip).
		Scan(&totalWeight)

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "获取成功",
		"data": gin.H{
			"today": bargeShipData,
			"total": totalWeight,
		},
	})
}

// 传入渔船号和日期，获取该驳船号在该日期的总打捞量和历史全部打捞量
type FisherShipDataQj struct {
	Date       string `json:"date"`
	FisherShip string `json:"fisherShip"`
}

func (ctl *chartCtl) GetFisherShipDataByDate(c *gin.Context) {
	ctl.db = models.Db
	var req FisherShipDataQj
	_ = c.ShouldBind(&req)
	//查询指定日期范围weight和disposal
	var fisherShipData []BargeShipData
	ctl.db.Table("record").
		Select("DATE(date) as date_only, fisher_ship as fisherShip, SUM(weight) as total_weight").
		Where("DATE(date) = ? AND fisher_ship = ?", req.Date, req.FisherShip).
		Group("date_only,fisherShip").
		Scan(&fisherShipData)

	//查询历史全部打捞量
	var totalWeight float64
	ctl.db.Table("record").
		Select("SUM(weight) as total_weight").
		Where("fisher_ship = ?", req.FisherShip).
		Scan(&totalWeight)

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "获取成功",
		"data": gin.H{
			"today": fisherShipData,
			"total": totalWeight,
		},
	})
}

// 重新写点
type BargeShipInfo struct {
	Name          string  `json:"name"`
	TotalWeight   float64 `json:"total_weight"`
	TotalDisposal float64 `json:"total_disposal"`
	TotalCount    int     `json:"total_count"`
}
type FisherShipInfo struct {
	Name          string  `json:"name"`
	TotalWeight   float64 `json:"total_weight"`
	TotalDisposal float64 `json:"total_disposal"`
}
type AreaInfo struct {
	Name        string  `json:"name"`
	TotalWeight float64 `json:"total_weight"`
}
type CompleteData struct {
	Date            string           `json:"date"`
	TotalWeight     float64          `json:"total_weight"`
	TotalDisposal   float64          `json:"total_disposal"`
	TotalCount      int              `json:"total_count"`
	BargeShipInfos  []BargeShipInfo  `json:"barge_ship_infos"`
	FisherShipInfos []FisherShipInfo `json:"fisher_ship_infos"`
}

// 获取某一段时间内的所有数据，一天的数据从02:00到次日02:00
func (ctl *chartCtl) GetCompleteData(c *gin.Context) {
	ctl.db = models.Db
	var req chartOneReq
	_ = c.ShouldBind(&req)
	req.StartDate = strings.ReplaceAll(req.StartDate, "T", " ")
	req.EndDate = strings.ReplaceAll(req.EndDate, "T", " ")
	//查询指定日期范围weight和disposal
	var completeData []CompleteData
	ctl.db.Table("record").
		Select("DATE(DATE_SUB(date, INTERVAL 2 HOUR)) as date, SUM(weight) as total_weight, SUM(disposal) as total_disposal, COUNT(*) as total_count").
		Where("DATE_FORMAT(DATE_SUB(date, INTERVAL 2 HOUR), '%Y-%m-%d %H') >= ? AND DATE_FORMAT(DATE_SUB(date, INTERVAL 2 HOUR), '%Y-%m-%d %H') <= ? AND state = ?", req.StartDate, req.EndDate, "完成").
		Group("DATE(DATE_SUB(date, INTERVAL 2 HOUR))").
		Scan(&completeData)

	for i := 0; i < len(completeData); i++ {
		completeData[i].Date = completeData[i].Date[0:10]
		// 获取驳船信息
		var bargeShipInfos []BargeShipInfo
		ctl.db.Table("record").
			Select("barge_ship as name, SUM(weight) as total_weight, SUM(disposal) as total_disposal, COUNT(*) as total_count").
			Where("DATE_FORMAT(DATE_SUB(date, INTERVAL 2 HOUR), '%Y-%m-%d') = ? AND state = ?", completeData[i].Date, "完成").
			Group("barge_ship").
			Scan(&bargeShipInfos)
		completeData[i].BargeShipInfos = bargeShipInfos

		// 获取渔船信息
		var fisherShipInfos []FisherShipInfo
		ctl.db.Table("record").
			Select("fisher_ship as name, SUM(weight) as total_weight, SUM(disposal) as total_disposal").
			Where("DATE_FORMAT(DATE_SUB(date, INTERVAL 2 HOUR), '%Y-%m-%d') = ? AND state = ?", completeData[i].Date, "完成").
			Group("fisher_ship").
			Find(&fisherShipInfos)
		completeData[i].FisherShipInfos = fisherShipInfos
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "获取成功",
		"data": completeData,
	})
}

// 获取历史全部的weight，disposal，count
type TotalData struct {
	TotalWeight   float64 `json:"total_weight"`
	TotalDisposal float64 `json:"total_disposal"`
	TotalCount    int     `json:"total_count"`
	//	每艘船的总打捞量
	BargeShipInfos  []BargeShipInfo  `json:"barge_ship_infos"`
	FisherShipInfos []FisherShipInfo `json:"fisher_ship_infos"`
}

func (ctl *chartCtl) GetTotalData(c *gin.Context) {
	ctl.db = models.Db
	var totalData TotalData

	// 获取总打捞量
	ctl.db.Table("record").
		Select("SUM(weight) as total_weight, COUNT(*) as total_count").
		Where("state = ?", "完成").
		Scan(&totalData)

	//在disposal_record中获取总处置量
	ctl.db.Table("disposal_record").
		Select("SUM(weight) as total_disposal").
		Scan(&totalData)

	// 获取每艘船的总打捞量
	var shipInfos []BargeShipInfo
	ctl.db.Table("record").
		Select("barge_ship as name, SUM(weight) as total_weight, COUNT(*) as total_count").
		Where("state = ?", "完成").
		Group("barge_ship").
		Scan(&shipInfos)

	//在disposal_record中获取每艘船的总处置量
	for i := 0; i < len(shipInfos); i++ {
		var disposal float64
		ctl.db.Table("disposal_record").
			Select("SUM(weight) as total_disposal").
			Where("barge_ship = ?", shipInfos[i].Name).
			Scan(&disposal)
		shipInfos[i].TotalDisposal = disposal
	}
	totalData.BargeShipInfos = shipInfos

	// 获取渔船信息
	var fisherShipInfos []FisherShipInfo
	ctl.db.Table("record").
		Select("fisher_ship as name, SUM(weight) as total_weight, SUM(disposal) as total_disposal").
		Where("state = ?", "完成").
		Group("fisher_ship").
		Find(&fisherShipInfos)

	totalData.FisherShipInfos = fisherShipInfos

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "获取成功",
		"data": totalData,
	})
}

type CompleteDataOnly struct {
	TotalWeight     float64          `json:"total_weight"`
	TotalDisposal   float64          `json:"total_disposal"`
	TotalCount      int              `json:"total_count"`
	BargeShipInfos  []BargeShipInfo  `json:"barge_ship_infos"`
	FisherShipInfos []FisherShipInfo `json:"fisher_ship_infos"`
}

// 获取一段时间内的所有数据
func (ctl *chartCtl) GetCompleteDataOnly(c *gin.Context) {
	ctl.db = models.Db
	var req chartOneReq
	_ = c.ShouldBind(&req)
	req.StartDate = strings.ReplaceAll(req.StartDate, "T", " ")
	req.EndDate = strings.ReplaceAll(req.EndDate, "T", " ")

	//查询指定日期范围weight和disposal
	var completeData CompleteDataOnly
	ctl.db.Table("record").
		Select("SUM(weight) as total_weight, COUNT(*) as total_count").
		Where("DATE_FORMAT(date, '%Y-%m-%d %H:%i') >= ? AND DATE_FORMAT(date, '%Y-%m-%d %H:%i') <= ? AND state = ?", req.StartDate, req.EndDate, "完成").
		Scan(&completeData)

	//在disposal_record中获取总处置量
	ctl.db.Table("disposal_record").
		Select("SUM(weight) as total_disposal").
		Where("DATE_FORMAT(date, '%Y-%m-%d %H:%i') >= ? AND DATE_FORMAT(date, '%Y-%m-%d %H:%i') <= ?", req.StartDate, req.EndDate).
		Scan(&completeData)

	// 获取驳船信息
	var bargeShipInfos []BargeShipInfo
	ctl.db.Table("record").
		Select("barge_ship as name, SUM(weight) as total_weight, COUNT(*) as total_count").
		Where("DATE_FORMAT(date, '%Y-%m-%d %H:%i') >= ? AND DATE_FORMAT(date, '%Y-%m-%d %H:%i') <= ? AND state = ?", req.StartDate, req.EndDate, "完成").
		Group("barge_ship").
		Scan(&bargeShipInfos)
	completeData.BargeShipInfos = bargeShipInfos

	// 获取渔船信息
	var fisherShipInfos []FisherShipInfo
	ctl.db.Table("record").
		Select("fisher_ship as name, SUM(weight) as total_weight, SUM(disposal) as total_disposal").
		Where("DATE_FORMAT(date, '%Y-%m-%d %H:%i') >= ? AND DATE_FORMAT(date, '%Y-%m-%d %H:%i') <= ? AND state = ?", req.StartDate, req.EndDate, "完成").
		Group("fisher_ship").
		Find(&fisherShipInfos)
	completeData.FisherShipInfos = fisherShipInfos

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "获取成功",
		"data": completeData,
	})
}
